package com.woniuxy.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.util.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//登陆失败处理器
public class LoginErrorHandler implements AuthenticationFailureHandler {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf8");

        String json = objectMapper.writeValueAsString(Result.fail(500,e.getMessage(),null));
        response.getWriter().write(json);
        response.getWriter().flush();
    }
}
